package com.zs.grain.ui

import android.app.Application
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import com.zs.grain.device.AdjustBean
import com.xnkj1688.semplibrary.mvvm.BaseViewModel
import com.zs.grain.service.*
import com.xnkj1688.semplibrary.entity.Adjustment
import com.zslibrary.BusMaster.BusMaster
import com.zslibrary.Enum.TestTypeDefine
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject


/**
 * Created by Wesley on 2017/7/17.
 */

class TestViewModel @Inject constructor(
    val mApp: Application,
    val mDeviceService: DeviceService,
    val mDataService: DataService,
    val mSignalRService: SignalRService
) : BaseViewModel(mApp) {

    val function = MutableLiveData<EnumFunction>()
    val componentViewModels = MutableLiveData<MutableList<TestItemViewModel>>()
    val testInfoViewModels = MutableLiveData<MutableList<TestItemViewModel>>()
    val adjustmentViewModels = MutableLiveData<MutableList<TestItemViewModel>>()

    private lateinit var adjustments: Array<AdjustBean>

    init {
        function.value = EnumFunction.TESTINFO
        var components = mDeviceService.components
        var list = ArrayList<TestItemViewModel>()
        for (item in components) {
            var model = TestItemViewModel(mApp)
            model.id = item.id.toInt()
            model.name = item.name //+ "( " + item.id.toString() + " )"
            list.add(model)
        }
        componentViewModels.value = list
        var testIV = TestItemViewModel(mApp)
        testIV.componentId = 0
        testIV.id = 2001
        testIV.name = "auxiliary"
        testIV.type = 0
        if (!mDataService.isEmulator)
            onClickComponent(testIV)
    }

    var lastComponentName = ""
    fun onClickComponent(vm: TestItemViewModel) {
        Single.just(vm)
            .subscribeOn(Schedulers.io())
            .subscribe({
                var testInfos = mDeviceService.getTestInfos(vm.id)
                var list1 = ArrayList<TestItemViewModel>()
                for (item in testInfos) {
                    var model = TestItemViewModel(mApp)
                    model.id = item.index
                    model.componentId = vm.id
                    model.name = item.name
                    model.type = item.type
                    list1.add(model)
                    lastComponentName = vm.name//jerry 2.23 加到日志显示
                }
                testInfoViewModels.postValue(list1)

                this.adjustments = mDeviceService.getAdjustments(vm.id)
                var list2 = ArrayList<TestItemViewModel>()
                for (item in adjustments) {
                    var model = TestItemViewModel(mApp)
                    model.id = item.id
                    model.componentId = vm.id
                    model.name = item.title + " ( " + (item.value/(Math.pow(10.0,item.decPoint.toDouble()))).toString() + " )"
                    list2.add(model)
                }
                if (list2.size == 0) {
                    var model = TestItemViewModel(mApp)
                    model.id = 0
                    model.componentId = 2000
                    model.name = "---暂无数据---"// + " ( " + item.value.toString() + " )"
                    list2.add(model)
                }
                adjustmentViewModels.postValue(list2)
            }, {
                it.printStackTrace()
            })
    }

    fun onClickTestInfoButton() {
        function.value = EnumFunction.TESTINFO
    }

    fun onClickAdjustmentButton() {
        function.value = EnumFunction.ADJUSTMENT
    }

    fun onClickTestInfo(vm: TestItemViewModel) {
        Single.just(vm)
            .subscribeOn(Schedulers.io())
            .subscribe({
                if (vm.type == TestTypeDefine.CALIBRATION_TEST) {
                    val notify = super.getNotify("Calibration")
                    notify.putInt("ComponentId", vm.componentId)
                    notify.putInt("FunctionIndex", vm.id)
                    notify.putString("Name", lastComponentName + " " + vm.name)
                    this.uiNotify.postValue(notify)
                } else if (vm.type == TestTypeDefine.DYNAMIC_TEST || vm.type == TestTypeDefine.SINGLE_TEST || vm.type == TestTypeDefine.STATIC_TEST) {
                    val notify = super.getNotify("SelectDialog")
                    notify.putInt("ComponentId", vm.componentId)
                    notify.putInt("Id", vm.id)//vm.componentId.toString()+" "+vm.name
                    notify.putString("Name", lastComponentName + " " + vm.name)
                    this.uiNotify.postValue(notify)
                } else if (vm.type == TestTypeDefine.DURAT_TEST) {
                    val notify = super.getNotify("Duration")
                    notify.putInt("ComponentId", vm.componentId)
                    notify.putInt("Id", vm.id)
                    notify.putString("Name", lastComponentName + " " + vm.name)
                    this.uiNotify.postValue(notify)
//                    val notify = super.getNotify("DurationTest")
//                    notify.putInt("ComponentId", vm.componentId)
//                    notify.putInt("Id", vm.id)
//                    notify.putString("Name", lastComponentName+" "+vm.name)
//                    this.uiNotify.postValue(notify)
                }
            }, {
                it.printStackTrace()
            })
    }


    fun startTest(componentyId: Int, id: Int) {
        Single.just(id)
            .subscribeOn(Schedulers.io())
            .subscribe({
                mDeviceService.testComponent(componentyId, id)
                //Log.d("kjw"+componentyId.toString(),id.toString())
                this.uiNotify.postValue(this.getNotify("HideProgress"))
            }, {
                it.printStackTrace()
            })
    }

    fun onClickAdjustment(vm: TestItemViewModel) {
        var notify = this.getNotify("Adjustment")
        notify.putInt("ComponentId", vm.componentId)
        notify.putInt("AdjustmentId", vm.id)
        this.uiNotify.value = notify
    }

    fun calibrate(componentId: Int, function: Int, times: Int) {
        Single.just(times)
            .subscribeOn(Schedulers.io())
            .subscribe({
                this.uiNotify.postValue(this.getNotify("ShowProgress"))
                for (i in 1..times) {
                    mDeviceService.testComponent(componentId, function)
                    Thread.sleep(4000)
                }
                this.uiNotify.postValue(this.getNotify("HideProgress"))
            }, {
                it.printStackTrace()
            })
    }

    fun saveCalibration(componentyId: Int, times: Int, value: Int) {
        var adjustment = mDeviceService.saveCalibration(componentyId, times, value)
        mSignalRService.uploadAdjustment(adjustment)
            .subscribe({}, {
                postToast("上传云端异常：" + it.message, Toast.LENGTH_LONG)
            })
    }

    fun saveAdjustment(componentId: Int, adjustmentId: Int, value: Int) {
        var adjustment = Adjustment()
        adjustment.componentId = componentId.toLong()
        adjustment.adjustmentId = adjustmentId.toLong()
        adjustment.value = value
        mDeviceService.saveAdjustment(adjustment)
        mSignalRService.uploadAdjustment(adjustment)
            .subscribe({}, {
                postToast("上传云端异常：" + it.message, Toast.LENGTH_LONG)
            })
        this.adjustments = mDeviceService.getAdjustments(componentId)
    }

    fun getAdjustBean(adjustmentId: Int): AdjustBean? =
        adjustments.find { x -> x.id == adjustmentId }

    enum class EnumFunction {
        TESTINFO,
        ADJUSTMENT
    }
}
